import 'package:flutter/material.dart';

class SnackBarWidget extends StatelessWidget {
  const SnackBarWidget({super.key});

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: const Text("子树中获取State对象"),
      ),
      body: Center(
        child: Builder(builder: (context){
          return ElevatedButton(onPressed:() {
            // 查找父级最近的Scaffold对应的ScaffoldState对象
            ScaffoldState state = Scaffold.of(context);
            // state.showSnackBar(
            //     const SnackBar(content: Text("我是SnackBar"))
            // );
          },
            child: const Text("显示SnackBar"),
          );
        },
        ),
      ),
    );
  }

}